home *** CD-ROM | disk | FTP | other *** search
/ Aminet 23 / Aminet 23 (1998)(GTI - Schatztruhe)[!][Feb 1998].iso / Aminet / misc / emu / amiSPIMsrc.lha / inst.c < prev    next >
C/C++ Source or Header  |  1994-01-17  |  39KB  |  1,820 lines

  1. /* SPIM S20 MIPS simulator.
  2.    Code to build assembly instructions and resolve symbolic labels.
  3.    Copyright (C) 1990-1994 by James Larus (larus@cs.wisc.edu).
  4.    ALL RIGHTS RESERVED.
  5.  
  6.    SPIM is distributed under the following conditions:
  7.  
  8.      You may make copies of SPIM for your own use and modify those copies.
  9.  
  10.      All copies of SPIM must retain my name and copyright notice.
  11.  
  12.      You may not sell SPIM or distributed SPIM in conjunction with a
  13.      commerical product or service without the expressed written consent of
  14.      James Larus.
  15.  
  16.    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  19.    PURPOSE. */
  20.  
  21.  
  22. /* $Header: /home/primost/larus/Software/SPIM/RCS/inst.c,v 3.32 1994/01/18 03:21:45 larus Exp larus $
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27.  
  28. #include "spim.h"
  29. #include "spim-utils.h"
  30. #include "inst.h"
  31. #include "mem.h"
  32. #include "reg.h"
  33. #include "sym-tbl.h"
  34. #include "y.tab.h"
  35. #include "parser.h"
  36. #include "scanner.h"
  37. #include "data.h"
  38.  
  39.  
  40. #ifdef __STDC__
  41. int bcmp (const void *, const void *, size_t);
  42. #else
  43. int bcmp ();
  44. #endif
  45.  
  46. /* Local functions: */
  47.  
  48. #ifdef __STDC__
  49. static int compare_pair_value (inst_info *p1, inst_info *p2);
  50. static void i_type_inst_full_word (int opcode, int rt, int rs, imm_expr *expr,
  51.                    int value_known, long int value);
  52. static void inst_cmp (instruction *inst1, instruction *inst2);
  53. static instruction *make_r_type_inst (int opcode, int rd, int rs, int rt);
  54. static instruction *mk_i_inst (unsigned long value, int opcode, int rs,
  55.                    int rt, int offset);
  56. static instruction *mk_j_inst (unsigned long value, int opcode, int target);
  57. static instruction *mk_r_inst (unsigned long value, int opcode, int rs,
  58.                    int rt, int rd, int shamt);
  59. static int print_imm_expr (char *buf, imm_expr *expr, int base_reg);
  60. static void sort_name_table (void);
  61. #else
  62. static int compare_pair_value ();
  63. static void i_type_inst_f ();
  64. static void i_type_inst_full_word ();
  65. static void inst_cmp ();
  66. static instruction * make_r_type_inst ();
  67. static instruction *mk_i_inst ();
  68. static instruction *mk_j_inst ();
  69. static instruction *mk_r_inst ();
  70. static int print_imm_expr ();
  71. static void sort_name_table ();
  72. #endif
  73.  
  74.  
  75. /* Local variables: */
  76.  
  77. /* Non-zero means store instructions in kernel, not user, text segment */
  78.  
  79. static int in_kernel = 0;
  80.  
  81. /* Instruction used as breakpoint by SPIM: */
  82.  
  83. static instruction *break_inst = NULL;
  84.  
  85.  
  86. /* Locations for next instruction in user and kernel text segments */
  87.  
  88. static mem_addr next_text_pc;
  89.  
  90. static mem_addr next_k_text_pc;
  91.  
  92.  
  93. #define INST_PC (in_kernel ? next_k_text_pc : next_text_pc)
  94. #define BUMP_INST_PC(DELTA) {if (in_kernel) \
  95.                    next_k_text_pc += DELTA; \
  96.                    else next_text_pc += DELTA;}
  97.  
  98.  
  99.  
  100. /* Set ADDRESS at which the next instruction is stored. */
  101.  
  102. #ifdef __STDC__
  103. void
  104. text_begins_at_point (mem_addr addr)
  105. #else
  106. void
  107. text_begins_at_point (addr)
  108.      mem_addr addr;
  109. #endif
  110. {
  111.   next_text_pc = addr;
  112. }
  113.  
  114.  
  115. #ifdef __STDC__
  116. void
  117. k_text_begins_at_point (mem_addr addr)
  118. #else
  119. void
  120. k_text_begins_at_point (addr)
  121.      mem_addr addr;
  122. #endif
  123. {
  124.   next_k_text_pc = addr;
  125. }
  126.  
  127.  
  128. /* Set the location (in user or kernel text space) for the next instruction. */
  129.  
  130. #ifdef __STDC__
  131. void
  132. set_text_pc (mem_addr addr)
  133. #else
  134. void
  135. set_text_pc (addr)
  136.      mem_addr addr;
  137. #endif
  138. {
  139.   if (in_kernel)
  140.     next_k_text_pc = addr;
  141.   else
  142.     next_text_pc = addr;
  143. }
  144.  
  145.  
  146. /* Return address for next instruction, in appropriate text segment. */
  147.  
  148. #ifdef __STDC__
  149. mem_addr
  150. current_text_pc (void)
  151. #else
  152. mem_addr
  153. current_text_pc ()
  154. #endif
  155. {
  156.   return (INST_PC);
  157. }
  158.  
  159.  
  160. /* Increment the current text segement PC. */
  161.  
  162. #ifdef __STDC__
  163. void
  164. increment_text_pc (int delta)
  165. #else
  166. void
  167. increment_text_pc (delta)
  168.      int delta;
  169. #endif
  170. {
  171.   BUMP_INST_PC (delta);
  172. }
  173.  
  174.  
  175. /* If FLAG is non-zero, next instruction goes to kernel text segment,
  176.    otherwise it goes to user segment. */
  177.  
  178. #ifdef __STDC__
  179. void
  180. user_kernel_text_segment (int to_kernel)
  181. #else
  182. void
  183. user_kernel_text_segment (to_kernel)
  184.      int to_kernel;
  185. #endif
  186. {
  187.   in_kernel = to_kernel;
  188. }
  189.  
  190.  
  191. /* Store an INSTRUCTION in memory at the next location. */
  192.  
  193. #ifdef __STDC__
  194. void
  195. store_instruction (instruction *inst)
  196. #else
  197. void
  198. store_instruction (inst)
  199.      instruction *inst;
  200. #endif
  201. {
  202.   if (data_dir)
  203.     {
  204.       store_word (inst_encode (inst));
  205.       free_inst (inst);
  206.     }
  207.   else if (text_dir)
  208.     {
  209.       exception_occurred = 0;
  210.       SET_MEM_INST (INST_PC, inst);
  211.       if (exception_occurred)
  212.     error ("Invalid address (0x%08x) for instruction\n", INST_PC);
  213.       else
  214.     BUMP_INST_PC (BYTES_PER_WORD);
  215.       if (inst != NULL)
  216.     {
  217.       SOURCE (inst) = source_line ();
  218.       if (ENCODING (inst) == 0)
  219.         ENCODING (inst) = inst_encode (inst);
  220.     }
  221.     }
  222. }
  223.  
  224.  
  225.  
  226. #ifdef __STDC__
  227. void
  228. i_type_inst_free (int opcode, int rt, int rs, imm_expr *expr)
  229. #else
  230. void
  231. i_type_inst_free (opcode, rt, rs, expr)
  232.      int opcode, rt, rs;
  233.      imm_expr *expr;
  234. #endif
  235. {
  236.   i_type_inst (opcode, rt, rs, expr);
  237.   free (expr);
  238. }
  239.  
  240.  
  241. /* Produce an immediate instruction with the OPCODE, RT, RS, and IMM
  242.    fields.  NB, because the immediate value may not fit in the field,
  243.    this routine may produce more than one instruction.    On the bare
  244.    machine, we resolve symbolic address, but they better produce values
  245.    that fit into instruction's immediate field. */
  246.  
  247. #ifdef __STDC__
  248. void
  249. i_type_inst (int opcode, int rt, int rs, imm_expr *expr)
  250. #else
  251. void
  252. i_type_inst (opcode, rt, rs, expr)
  253.      int opcode, rt, rs;
  254.      imm_expr *expr;
  255. #endif
  256. {
  257.   instruction *inst = (instruction *) zmalloc (sizeof (instruction));
  258.  
  259.   OPCODE (inst) = opcode;
  260.   RS (inst) = rs;
  261.   RT (inst) = rt;
  262.   EXPR (inst) = copy_imm_expr (expr);
  263.   if (expr->symbol == NULL || SYMBOL_IS_DEFINED (expr->symbol))
  264.     {
  265.       /* Evaluate the instruction's expression. */
  266.       long value = eval_imm_expr (expr);
  267.  
  268.       if (!bare_machine
  269.       && (value & 0xffff0000) != 0
  270.       && !((value & 0xffff0000) == 0xffff0000
  271.            && (opcode == Y_ADDI_OP
  272.            || opcode == Y_ADDIU_OP
  273.            || opcode == Y_SLTI_OP
  274.            || opcode == Y_SLTIU_OP)))
  275.     {
  276.       free_inst (inst);
  277.       i_type_inst_full_word (opcode, rt, rs, expr, 1, value);
  278.       return;
  279.     }
  280.       else
  281.     resolve_a_label (expr->symbol, inst);
  282.     }
  283.   else if (bare_machine || expr->bits != 0)
  284.     /* Don't know expression's value, but only needed upper/lower 16-bits
  285.        anyways. */
  286.     record_inst_uses_symbol (inst, expr->symbol);
  287.   else
  288.     {
  289.       /* Don't know the expressions's value and want all of its bits,
  290.      so assume that it will not produce a small result and generate
  291.      sequence for 32 bit value. */
  292.       free_inst (inst);
  293.  
  294.       i_type_inst_full_word (opcode, rt, rs, expr, 0, 0);
  295.       return;
  296.     }
  297.  
  298.   store_instruction (inst);
  299. }
  300.  
  301.  
  302. /* The immediate value for an instruction will (or may) not fit in 16 bits.
  303.    Build the value from its piece with separate instructions. */
  304.  
  305. #ifdef __STDC__
  306. static void
  307. i_type_inst_full_word (int opcode, int rt, int rs, imm_expr *expr,
  308.                int value_known, long int value)
  309. #else
  310. static void
  311. i_type_inst_full_word (opcode, rt, rs, expr, value_known, value)
  312.      int opcode, rt, rs;
  313.      imm_expr *expr;
  314.      int value_known;
  315.      long value;
  316. #endif
  317. {
  318.   if (opcode_is_load_store (opcode))
  319.     {
  320.       long offset;
  321.  
  322.       if (expr->symbol != NULL
  323.       && expr->symbol->gp_flag && rs == 0
  324.       && (offset = expr->symbol->addr + expr->offset) >= -32*K
  325.       && offset < 32*K)
  326.     i_type_inst_free (opcode, rt, REG_GP, make_imm_expr (offset, NULL, 0));
  327.       else if (value_known)
  328.     {
  329.       int low, high;
  330.  
  331.       high = (value >> 16) & 0xffff;
  332.       low = 0xffff & value;
  333.  
  334.       if (high &&
  335.           !(high == 0xffff && (low & 0x8000))) /* load sign-extends */
  336.         {
  337.           if (low & 0x8000)
  338.         high += 1;        /* Adjust since load sign-extends */
  339.  
  340.           i_type_inst_free (Y_LUI_OP, 1, 0, const_imm_expr (high));
  341.           if (rs != 0)    /* Base register */
  342.         r_type_inst (Y_ADDU_OP, 1, 1, rs);
  343.           i_type_inst_free (opcode, rt, 1, const_imm_expr (low));
  344.         }
  345.       else
  346.         i_type_inst_free (opcode, rt, rs, const_imm_expr (low));
  347.     }
  348.       else
  349.     {
  350.       /* Use $at */
  351.       /* Need to adjust if lower bits are negative */
  352.       i_type_inst_free (Y_LUI_OP, 1, 0, upper_bits_of_expr (expr));
  353.       if (rs != 0)        /* Base register */
  354.         r_type_inst (Y_ADDU_OP, 1, 1, rs);
  355.       i_type_inst_free (opcode, rt, 1, lower_bits_of_expr (expr));
  356.     }
  357.     }
  358.   else if (opcode_is_branch (opcode))
  359.     {
  360.       /* This only allows branches +/- 32K, which is not correct! */
  361.       i_type_inst_free (opcode, rt, rs, lower_bits_of_expr (expr));
  362.     }
  363.   else
  364.     /* Computation instruction */
  365.     {
  366.       long offset;
  367.  
  368.       if (expr->symbol != NULL
  369.       && expr->symbol->gp_flag && rs == 0
  370.       && (offset = expr->symbol->addr + expr->offset) >= -32*K
  371.       && offset < 32*K)
  372.     i_type_inst_free ((opcode == Y_LUI_OP ? Y_ADDIU_OP : opcode),
  373.                rt, REG_GP, make_imm_expr (offset, NULL, 0));
  374.       else
  375.     {
  376.       /* Use $at */
  377.       if ((opcode == Y_ORI_OP
  378.            || opcode == Y_ADDI_OP || opcode == Y_ADDIU_OP
  379.            || opcode == Y_LUI_OP)
  380.           && rs == 0)
  381.         {
  382.           if (value_known && (value & 0xffff) == 0)
  383.         i_type_inst_free (Y_LUI_OP, rt, 0, upper_bits_of_expr (expr));
  384.           else
  385.         {
  386.           i_type_inst_free (Y_LUI_OP, 1, 0, upper_bits_of_expr (expr));
  387.           i_type_inst_free (Y_ORI_OP, rt, 1, lower_bits_of_expr(expr));
  388.         }
  389.         }
  390.       else
  391.         {
  392.           i_type_inst_free (Y_LUI_OP, 1, 0, upper_bits_of_expr (expr));
  393.           i_type_inst_free (Y_ORI_OP, 1, 1, lower_bits_of_expr (expr));
  394.           r_type_inst (imm_op_to_op (opcode), rt, rs, 1);
  395.         }
  396.     }
  397.     }
  398. }
  399.  
  400.  
  401. /* Return a jump-type instruction with the given OPCODE and TARGET
  402.    fields. NB, even the immediate value may not fit in the field, this
  403.    routine will not produce more than one instruction. */
  404.  
  405. #ifdef __STDC__
  406. void
  407. j_type_inst (int opcode, imm_expr *target)
  408. #else
  409. void
  410. j_type_inst (opcode, target)
  411.      int opcode;
  412.      imm_expr *target;
  413. #endif
  414. {
  415.   instruction *inst = (instruction *) zmalloc (sizeof (instruction));
  416.  
  417.   OPCODE(inst) = opcode;
  418.   target->offset = 0;        /* Not PC relative */
  419.   target->pc_relative = 0;
  420.   EXPR (inst) = copy_imm_expr (target);
  421.   if (target->symbol == NULL || SYMBOL_IS_DEFINED (target->symbol))
  422.     resolve_a_label (target->symbol, inst);
  423.   else
  424.     record_inst_uses_symbol (inst, target->symbol);
  425.   store_instruction (inst);
  426. }
  427.  
  428.  
  429. /* Return a register-type instruction with the given OPCODE, RD, RS, and RT
  430.    fields. */
  431.  
  432. #ifdef __STDC__
  433. static instruction *
  434. make_r_type_inst (int opcode, int rd, int rs, int rt)
  435. #else
  436. static instruction *
  437. make_r_type_inst (opcode, rd, rs, rt)
  438.      int opcode, rd, rs, rt;
  439. #endif
  440. {
  441.   instruction *inst = (instruction *) zmalloc (sizeof (instruction));
  442.  
  443.   OPCODE(inst) = opcode;
  444.   RS(inst) = rs;
  445.   RT(inst) = rt;
  446.   RD(inst) = rd;
  447.   SHAMT(inst) = 0;
  448.   return (inst);
  449. }
  450.  
  451.  
  452. /* Return a register-type instruction with the given OPCODE, RD, RS, and RT
  453.    fields. */
  454.  
  455. #ifdef __STDC__
  456. void
  457. r_type_inst (int opcode, int rd, int rs, int rt)
  458. #else
  459. void
  460. r_type_inst (opcode, rd, rs, rt)
  461.      int opcode, rd, rs, rt;
  462. #endif
  463. {
  464.   store_instruction (make_r_type_inst (opcode, rd, rs, rt));
  465. }
  466.  
  467.  
  468. /* Return a register-shift instruction with the given OPCODE, RD, RT, and
  469.    SHAMT fields.*/
  470.  
  471. #ifdef __STDC__
  472. void
  473. r_sh_type_inst (int opcode, int rd, int rt, int shamt)
  474. #else
  475. void
  476. r_sh_type_inst (opcode, rd, rt, shamt)
  477.      int opcode, rd, rt, shamt;
  478. #endif
  479. {
  480.   instruction *inst = make_r_type_inst (opcode, rd, 0, rt);
  481.  
  482.   SHAMT(inst) = shamt & 0x1f;
  483.   store_instruction (inst);
  484. }
  485.  
  486.  
  487. /* Return a floating-point compare instruction with the given OPCODE,
  488.    FS, and FT fields.*/
  489.  
  490. #ifdef __STDC__
  491. void
  492. r_cond_type_inst (int opcode, int rs, int rt)
  493. #else
  494. void
  495. r_cond_type_inst (opcode, rs, rt)
  496.      int opcode, rs, rt;
  497. #endif
  498. {
  499.   instruction *inst = make_r_type_inst (opcode, 0, rs, rt);
  500.  
  501.   switch (opcode)
  502.     {
  503.     case Y_C_EQ_D_OP:
  504.     case Y_C_EQ_S_OP:
  505.       {
  506.     COND(inst) = COND_EQ;
  507.     break;
  508.       }
  509.  
  510.     case Y_C_LE_D_OP:
  511.     case Y_C_LE_S_OP:
  512.       {
  513.     COND(inst) = COND_IN | COND_LT | COND_EQ;
  514.     break;
  515.       }
  516.  
  517.     case Y_C_LT_D_OP:
  518.     case Y_C_LT_S_OP:
  519.       {
  520.     COND(inst) = COND_IN | COND_LT;
  521.     break;
  522.       }
  523.  
  524.     case Y_C_NGE_D_OP:
  525.     case Y_C_NGE_S_OP:
  526.       {
  527.     COND(inst) = COND_IN | COND_LT | COND_UN;
  528.     break;
  529.       }
  530.  
  531.     case Y_C_NGLE_D_OP:
  532.     case Y_C_NGLE_S_OP:
  533.       {
  534.     COND(inst) = COND_IN | COND_UN;
  535.     break;
  536.       }
  537.  
  538.     case Y_C_NGL_D_OP:
  539.     case Y_C_NGL_S_OP:
  540.       {
  541.     COND(inst) = COND_IN | COND_EQ | COND_UN;
  542.     break;
  543.       }
  544.  
  545.     case Y_C_NGT_D_OP:
  546.     case Y_C_NGT_S_OP:
  547.       {
  548.     COND(inst) = COND_IN | COND_LT | COND_EQ | COND_UN;
  549.     break;
  550.       }
  551.  
  552.     case Y_C_OLE_D_OP:
  553.     case Y_C_OLE_S_OP:
  554.       {
  555.     COND(inst) = COND_LT | COND_EQ;
  556.     break;
  557.       }
  558.  
  559.     case Y_C_SEQ_D_OP:
  560.     case Y_C_SEQ_S_OP:
  561.       {
  562.     COND(inst) = COND_IN | COND_EQ;
  563.     break;
  564.       }
  565.  
  566.     case Y_C_SF_D_OP:
  567.     case Y_C_SF_S_OP:
  568.       {
  569.     COND(inst) = COND_IN;
  570.     break;
  571.       }
  572.  
  573.     case Y_C_F_D_OP:
  574.     case Y_C_F_S_OP:
  575.       {
  576.     COND(inst) = 0;
  577.     break;
  578.       }
  579.  
  580.     case Y_C_UEQ_D_OP:
  581.     case Y_C_UEQ_S_OP:
  582.       {
  583.     COND(inst) = COND_EQ | COND_UN;
  584.     break;
  585.       }
  586.  
  587.     case Y_C_ULE_D_OP:
  588.     case Y_C_ULE_S_OP:
  589.       {
  590.     COND(inst) = COND_LT | COND_EQ | COND_UN;
  591.     break;
  592.       }
  593.  
  594.     case Y_C_UN_D_OP:
  595.     case Y_C_UN_S_OP:
  596.       {
  597.     COND(inst) = COND_UN;
  598.     break;
  599.       }
  600.     }
  601.   store_instruction (inst);
  602. }
  603.  
  604.  
  605. /* Make and return a deep copy of INST. */
  606.  
  607. #ifdef __STDC__
  608. instruction *
  609. copy_inst (instruction *inst)
  610. #else
  611. instruction *
  612. copy_inst (inst)
  613. instruction *inst;
  614. #endif
  615. {
  616.   instruction *new_inst = (instruction *) xmalloc (sizeof (instruction));
  617.  
  618.   bcopy (inst, new_inst, sizeof (instruction));
  619.   EXPR (new_inst) = copy_imm_expr (EXPR (inst));
  620.   return (new_inst);
  621. }
  622.  
  623.  
  624. #ifdef __STDC__
  625. void
  626. free_inst (instruction *inst)
  627. #else
  628. void
  629. free_inst (inst)
  630. instruction *inst;
  631. #endif
  632. {
  633.   if (inst != break_inst)
  634.     /* Don't free the breakpoint insructions since we only have one. */
  635.     {
  636.       if (EXPR (inst))
  637.     free (EXPR (inst));
  638.       free (inst);
  639.     }
  640. }
  641.  
  642.  
  643.  
  644. /* Maintain a table mapping from opcode to instruction name and
  645.    instruction type.
  646.  
  647.    Table must be sorted before first use since its entries are
  648.    alphabetical on name, not ordered by opcode. */
  649.  
  650. static int sorted_name_table = 0;    /* Non-zero => table sorted */
  651.  
  652.  
  653. /* Map from opcode -> name/type. */
  654.  
  655. static inst_info name_tbl [] = {
  656. #undef OP
  657. #define OP(NAME, OPCODE, TYPE, R_OPCODE) {NAME, OPCODE, TYPE},
  658. #include "op.h"
  659. };
  660.  
  661.  
  662. /* Compare the VALUE1 field of two INST_INFO entries in the format
  663.    required by qsort. */
  664.  
  665. #ifdef __STDC__
  666. static int
  667. compare_pair_value (inst_info *p1, inst_info *p2)
  668. #else
  669. static int
  670. compare_pair_value (p1, p2)
  671.      inst_info *p1, *p2;
  672. #endif
  673. {
  674.   if (p1->value1 < p2->value1)
  675.     return (-1);
  676.   else if (p1->value1 > p2->value1)
  677.     return (1);
  678.   else
  679.     return (0);
  680. }
  681.  
  682.  
  683. /* Sort the opcode table on their key (the opcode value). */
  684.  
  685. #ifdef __STDC__
  686. static void
  687. sort_name_table (void)
  688. #else
  689. static void
  690. sort_name_table ()
  691. #endif
  692. {
  693.   qsort (name_tbl,
  694.      sizeof (name_tbl) / sizeof (inst_info),
  695.      sizeof (inst_info),
  696.      (QSORT_FUNC) compare_pair_value);
  697.   sorted_name_table = 1;
  698. }
  699.  
  700.  
  701. /* Print the instruction stored at the memory ADDRESS. */
  702.  
  703. #ifdef __STDC__
  704. void
  705. print_inst (mem_addr addr)
  706. #else
  707. void
  708. print_inst (addr)
  709.      mem_addr addr;
  710. #endif
  711. {
  712.   instruction *inst;
  713.   char buf [128];
  714.  
  715.   exception_occurred = 0;
  716.   READ_MEM_INST (inst, addr);
  717.  
  718.   if (exception_occurred)
  719.     {
  720.       error ("Can't print instruction not in text segment (0x%08x)\n", addr);
  721.       return;
  722.     }
  723.   print_inst_internal (buf, 128, inst, addr);
  724.   write_output (message_out, buf);
  725. }
  726.  
  727.  
  728. #ifdef __STDC__
  729. int
  730. print_inst_internal (char *buf, int length, instruction *inst, mem_addr addr)
  731. #else
  732. int
  733. print_inst_internal (buf, length, inst, addr)
  734.      char *buf;
  735.      int length;
  736.      instruction *inst;
  737.      mem_addr addr;
  738. #endif
  739. {
  740.   char *bp = buf;
  741.   inst_info *entry;
  742.  
  743.   if (!sorted_name_table)
  744.     sort_name_table ();
  745.  
  746.   sprintf (buf, "[0x%08x]\t", addr);
  747.   buf += strlen (buf);
  748.   if (inst == NULL)
  749.     {
  750.       sprintf (buf, "<none>\n");
  751.       buf += strlen (buf);
  752.       return (buf - bp);
  753.     }
  754.   entry = map_int_to_inst_info (name_tbl,
  755.                 sizeof (name_tbl) / sizeof (inst_info),
  756.                 OPCODE (inst));
  757.   if (entry == NULL)
  758.     {
  759.       sprintf (buf, "<unknown instruction %d>\n", OPCODE (inst));
  760.       buf += strlen (buf);
  761.       return (buf - bp);
  762.     }
  763.   sprintf (buf, "0x%08x  %s", ENCODING (inst), entry->name);
  764.   buf += strlen (buf);
  765.   switch (entry->value2)
  766.     {
  767.     case B0_TYPE_INST:
  768.       sprintf (buf, " %d", SIGN_EX (IMM (inst) << 2));
  769.       buf += strlen (buf);
  770.       break;
  771.  
  772.     case B1_TYPE_INST:
  773.       sprintf (buf, " $%d %d", RS (inst), SIGN_EX (IMM (inst) << 2));
  774.       buf += strlen (buf);
  775.       break;
  776.  
  777.     case I1t_TYPE_INST:
  778.       sprintf (buf, " $%d, %d", RT (inst), IMM (inst));
  779.       buf += strlen (buf);
  780.       break;
  781.  
  782.     case I2_TYPE_INST:
  783.       sprintf (buf, " $%d, $%d, %d", RT (inst), RS (inst), IMM (inst));
  784.       buf += strlen (buf);
  785.       break;
  786.  
  787.     case B2_TYPE_INST:
  788.       sprintf (buf, " $%d, $%d, %d", RS (inst), RT (inst),
  789.            SIGN_EX (IMM (inst) << 2));
  790.       buf += strlen (buf);
  791.       break;
  792.  
  793.     case I2a_TYPE_INST:
  794.       sprintf (buf, " $%d, %d($%d)", RT (inst), IMM (inst), BASE (inst));
  795.       buf += strlen (buf);
  796.       break;
  797.  
  798.     case R1s_TYPE_INST:
  799.       sprintf (buf, " $%d", RS (inst));
  800.       buf += strlen (buf);
  801.       break;
  802.  
  803.     case R1d_TYPE_INST:
  804.       sprintf (buf, " $%d", RD (inst));
  805.       buf += strlen (buf);
  806.       break;
  807.  
  808.     case R2td_TYPE_INST:
  809.       sprintf (buf, " $%d, $%d", RT (inst), RD (inst));
  810.       buf += strlen (buf);
  811.       break;
  812.  
  813.     case R2st_TYPE_INST:
  814.       sprintf (buf, " $%d, $%d", RS (inst), RT (inst));
  815.       buf += strlen (buf);
  816.       break;
  817.  
  818.     case R2ds_TYPE_INST:
  819.       sprintf (buf, " $%d, $%d", RD (inst), RS (inst));
  820.       buf += strlen (buf);
  821.       break;
  822.  
  823.     case R2sh_TYPE_INST:
  824.       if (ENCODING (inst) == 0)
  825.     {
  826.       buf -= 3;        /* zap sll */
  827.       sprintf (buf, "nop");
  828.     }
  829.       else
  830.     sprintf (buf, " $%d, $%d, %d", RD (inst), RT (inst), SHAMT (inst));
  831.       buf += strlen (buf);
  832.       break;
  833.  
  834.     case R3_TYPE_INST:
  835.       sprintf (buf, " $%d, $%d, $%d", RD (inst), RS (inst), RT (inst));
  836.       buf += strlen (buf);
  837.       break;
  838.  
  839.     case R3sh_TYPE_INST:
  840.       sprintf (buf, " $%d, $%d, $%d", RD (inst), RT (inst), RS (inst));
  841.       buf += strlen (buf);
  842.       break;
  843.  
  844.     case FP_I2a_TYPE_INST:
  845.       sprintf (buf, " $f%d, %d($%d)", FT (inst), IMM (inst), BASE (inst));
  846.       buf += strlen (buf);
  847.       break;
  848.  
  849.     case FP_R2ds_TYPE_INST:
  850.       sprintf (buf, " $f%d, $f%d", FD (inst), FS (inst));
  851.       buf += strlen (buf);
  852.       break;
  853.  
  854.     case FP_R2st_TYPE_INST:
  855.       sprintf (buf, " $f%d, $f%d", FS (inst), FT (inst));
  856.       buf += strlen (buf);
  857.       break;
  858.  
  859.     case FP_R3_TYPE_INST:
  860.       sprintf (buf, " $f%d, $f%d, $f%d", FD (inst), FS (inst), FT (inst));
  861.       buf += strlen (buf);
  862.       break;
  863.  
  864.     case FP_MOV_TYPE_INST:
  865.       sprintf (buf, " $f%d, $f%d", FD (inst), FS (inst));
  866.       buf += strlen (buf);
  867.       break;
  868.  
  869.     case J_TYPE_INST:
  870.       sprintf (buf, " 0x%08x", TARGET (inst) << 2);
  871.       buf += strlen (buf);
  872.       break;
  873.  
  874.     case CP_TYPE_INST:
  875.       sprintf (buf, " $%d, $%d", RT (inst), RD (inst));
  876.       buf += strlen (buf);
  877.       break;
  878.  
  879.     case NOARG_TYPE_INST:
  880.       break;
  881.  
  882.     case ASM_DIR:
  883.     case PSEUDO_OP:
  884.     default:
  885.       fatal_error ("Unknown instruction type in print_inst\n");
  886.     }
  887.  
  888.   if (EXPR (inst) != NULL && EXPR (inst)->symbol != NULL)
  889.     {
  890.       sprintf (buf, " [");
  891.       buf += strlen (buf);
  892.       if (opcode_is_load_store (OPCODE (inst)))
  893.     buf += print_imm_expr (buf, EXPR (inst), BASE (inst));
  894.       else
  895.     buf += print_imm_expr (buf, EXPR (inst), -1);
  896.       sprintf (buf, "]");
  897.       buf += strlen (buf);
  898.     }
  899.  
  900.   if (SOURCE (inst) != NULL)
  901.     {
  902.       int line_length = buf - bp;
  903.       int comment_length = 2 + strlen (SOURCE (inst));
  904.  
  905.       /* Make sure comment (source line text) fits on current line. */
  906.       if (length > 50)
  907.     {
  908.       for ( ; line_length < 50; line_length ++)
  909.         {
  910.           sprintf (buf, " ");
  911.           buf += 1;
  912.         }
  913.       if (line_length + comment_length >= length)
  914.         SOURCE (inst) [length - line_length - 1] = '\0';
  915.       sprintf (buf, "; %s", SOURCE (inst));
  916.       buf += strlen (buf);
  917.     }
  918.     }
  919.   sprintf (buf, "\n");
  920.   buf += strlen (buf);
  921.   return (buf - bp);
  922. }
  923.  
  924.  
  925.  
  926. /* Return non-zero if an INSTRUCTION is a conditional branch. */
  927.  
  928. #ifdef __STDC__
  929. int
  930. opcode_is_branch (int opcode)
  931. #else
  932. int
  933. opcode_is_branch (opcode)
  934.      int opcode;
  935. #endif
  936. {
  937.   switch (opcode)
  938.     {
  939.     case Y_BEQ_OP:
  940.     case Y_BEQZ_POP:
  941.     case Y_BGE_POP:
  942.     case Y_BGEU_POP:
  943.     case Y_BGEZ_OP:
  944.     case Y_BGEZAL_OP:
  945.     case Y_BGT_POP:
  946.     case Y_BGTU_POP:
  947.     case Y_BGTZ_OP:
  948.     case Y_BLE_POP:
  949.     case Y_BLEU_POP:
  950.     case Y_BLEZ_OP:
  951.     case Y_BLT_POP:
  952.     case Y_BLTU_POP:
  953.     case Y_BLTZ_OP:
  954.     case Y_BLTZAL_OP:
  955.     case Y_BNE_OP:
  956.     case Y_BNEZ_POP:
  957.     case Y_BC1F_OP:
  958.     case Y_BC1T_OP:
  959.       return (1);
  960.  
  961.     default:
  962.       return (0);
  963.     }
  964. }
  965.  
  966.  
  967. /* Return non-zero if an INSTRUCTION is an conditional branch (jump). */
  968.  
  969. #ifdef __STDC__
  970. int
  971. opcode_is_jump (int opcode)
  972. #else
  973. int
  974. opcode_is_jump (opcode)
  975.      int opcode;
  976. #endif
  977. {
  978.   switch (opcode)
  979.     {
  980.     case Y_J_OP:
  981.     case Y_JAL_OP:
  982.       return (1);
  983.  
  984.     default:
  985.       return (0);
  986.     }
  987. }
  988.  
  989. /* Return non-zero if an INSTRUCTION is a load or store. */
  990.  
  991. #ifdef __STDC__
  992. int
  993. opcode_is_load_store (int opcode)
  994. #else
  995. int
  996. opcode_is_load_store (opcode)
  997.      int opcode;
  998. #endif
  999. {
  1000.   switch (opcode)
  1001.     {
  1002.     case Y_LB_OP: return (1);
  1003.     case Y_LBU_OP: return (1);
  1004.     case Y_LH_OP: return (1);
  1005.     case Y_LHU_OP: return (1);
  1006.     case Y_LW_OP: return (1);
  1007.     case Y_LWC0_OP: return (1);
  1008.     case Y_LWC1_OP: return (1);
  1009.     case Y_LWC2_OP: return (1);
  1010.     case Y_LWC3_OP: return (1);
  1011.     case Y_LWL_OP: return (1);
  1012.     case Y_LWR_OP: return (1);
  1013.     case Y_SB_OP: return (1);
  1014.     case Y_SH_OP: return (1);
  1015.     case Y_SW_OP: return (1);
  1016.     case Y_SWC0_OP: return (1);
  1017.     case Y_SWC1_OP: return (1);
  1018.     case Y_SWC2_OP: return (1);
  1019.     case Y_SWC3_OP: return (1);
  1020.     case Y_SWL_OP: return (1);
  1021.     case Y_SWR_OP: return (1);
  1022.     case Y_L_D_POP: return (1);
  1023.     case Y_L_S_POP: return (1);
  1024.     case Y_S_D_POP: return (1);
  1025.     case Y_S_S_POP: return (1);
  1026.     default: return (0);
  1027.     }
  1028. }
  1029.  
  1030.  
  1031. /* Return non-zero if a breakpoint is set at ADDR. */
  1032.  
  1033. #ifdef __STDC__
  1034. int
  1035. inst_is_breakpoint (mem_addr addr)
  1036. #else
  1037. int
  1038. inst_is_breakpoint (addr)
  1039.      mem_addr addr;
  1040. #endif
  1041. {
  1042.   instruction *old_inst;
  1043.  
  1044.   if (break_inst == NULL)
  1045.     break_inst = make_r_type_inst (Y_BREAK_OP, 1, 0, 0);
  1046.  
  1047.   READ_MEM_INST (old_inst, addr);
  1048.   return (old_inst == break_inst);
  1049. }
  1050.  
  1051.  
  1052. /* Set a breakpoint at ADDR and return the old instruction.  If the
  1053.    breakpoint cannot be set, return NULL. */
  1054.  
  1055. #ifdef __STDC__
  1056. instruction *
  1057. set_breakpoint (mem_addr addr)
  1058. #else
  1059. instruction *
  1060. set_breakpoint (addr)
  1061.      mem_addr addr;
  1062. #endif
  1063. {
  1064.   instruction *old_inst;
  1065.  
  1066.   if (break_inst == NULL)
  1067.     break_inst = make_r_type_inst (Y_BREAK_OP, 1, 0, 0);
  1068.  
  1069.   exception_occurred = 0;
  1070.   READ_MEM_INST (old_inst, addr);
  1071.   if (old_inst == break_inst)
  1072.     return (NULL);
  1073.   SET_MEM_INST (addr, break_inst);
  1074.   if (exception_occurred)
  1075.     return (NULL);
  1076.   else
  1077.     return (old_inst);
  1078. }
  1079.  
  1080.  
  1081.  
  1082. /* An immediate expression has the form: SYMBOL +/- IOFFSET, where either
  1083.    part may be omitted. */
  1084.  
  1085. /* Make and return a new immediate expression */
  1086.  
  1087. #ifdef __STDC__
  1088. imm_expr *
  1089. make_imm_expr (int offs, char *sym, int pc_rel)
  1090. #else
  1091. imm_expr *
  1092. make_imm_expr (offs, sym, pc_rel)
  1093.      int offs;
  1094.      char *sym;
  1095.      int pc_rel;
  1096. #endif
  1097. {
  1098.   imm_expr *expr = (imm_expr *) xmalloc (sizeof (imm_expr));
  1099.  
  1100.   expr->offset = offs;
  1101.   expr->bits = 0;
  1102.   expr->pc_relative = pc_rel;
  1103.   if (sym != NULL)
  1104.     expr->symbol = lookup_label (sym);
  1105.   else
  1106.     expr->symbol = NULL;
  1107.   return (expr);
  1108. }
  1109.  
  1110.  
  1111. /* Return a shallow copy of the EXPRESSION. */
  1112.  
  1113. #ifdef __STDC__
  1114. imm_expr *
  1115. copy_imm_expr (imm_expr *old_expr)
  1116. #else
  1117. imm_expr *
  1118. copy_imm_expr (old_expr)
  1119.      imm_expr *old_expr;
  1120. #endif
  1121. {
  1122.   imm_expr *expr = (imm_expr *) xmalloc (sizeof (imm_expr));
  1123.  
  1124.   bcopy (old_expr, expr, sizeof (imm_expr));
  1125.   return (expr);
  1126. }
  1127.  
  1128.  
  1129. /* Return a shallow copy of an EXPRESSION that only uses the upper
  1130.    sixteen bits of the expression's value. */
  1131.  
  1132. #ifdef __STDC__
  1133. imm_expr *
  1134. upper_bits_of_expr (imm_expr *old_expr)
  1135. #else
  1136. imm_expr *
  1137. upper_bits_of_expr (old_expr)
  1138.      imm_expr *old_expr;
  1139. #endif
  1140. {
  1141.   imm_expr *expr = copy_imm_expr (old_expr);
  1142.  
  1143.   expr->bits = 1;
  1144.   return (expr);
  1145. }
  1146.  
  1147.  
  1148. /* Return a shallow copy of the EXPRESSION that only uses the lower
  1149.    sixteen bits of the expression's value. */
  1150.  
  1151. #ifdef __STDC__
  1152. imm_expr *
  1153. lower_bits_of_expr (imm_expr *old_expr)
  1154. #else
  1155. imm_expr *
  1156. lower_bits_of_expr (old_expr)
  1157.      imm_expr *old_expr;
  1158. #endif
  1159. {
  1160.   imm_expr *expr = copy_imm_expr (old_expr);
  1161.  
  1162.   expr->bits = -1;
  1163.   return (expr);
  1164. }
  1165.  
  1166.  
  1167. /* Return an instruction expression for a constant VALUE. */
  1168.  
  1169. #ifdef __STDC__
  1170. imm_expr *
  1171. const_imm_expr (long int value)
  1172. #else
  1173. imm_expr *
  1174. const_imm_expr (value)
  1175.      long value;
  1176. #endif
  1177. {
  1178.   return (make_imm_expr (value, NULL, 0));
  1179. }
  1180.  
  1181.  
  1182. /* Return a shallow copy of the EXPRESSION with the offset field
  1183.    incremented by the given amount. */
  1184.  
  1185. #ifdef __STDC__
  1186. imm_expr *
  1187. incr_expr_offset (imm_expr *expr, long int value)
  1188. #else
  1189. imm_expr *
  1190. incr_expr_offset (expr, value)
  1191.      imm_expr *expr;
  1192.      long value;
  1193. #endif
  1194. {
  1195.   imm_expr *new_expr = copy_imm_expr (expr);
  1196.  
  1197.   new_expr->offset += value;
  1198.   return (new_expr);
  1199. }
  1200.  
  1201.  
  1202. /* Return the value of the EXPRESSION. */
  1203.  
  1204. #ifdef __STDC__
  1205. long
  1206. eval_imm_expr (imm_expr *expr)
  1207. #else
  1208. long
  1209. eval_imm_expr (expr)
  1210.      imm_expr *expr;
  1211. #endif
  1212. {
  1213.   long value;
  1214.  
  1215.   if (expr->symbol == NULL)
  1216.     value = expr->offset;
  1217.   else if (SYMBOL_IS_DEFINED (expr->symbol))
  1218.     {
  1219.       value = expr->offset + expr->symbol->addr;
  1220.       if (expr->symbol->gp_flag) /* Addr is offset from $gp */
  1221.     value += gp_midpoint;
  1222.     }
  1223.   else
  1224.     {
  1225.       error ("Evaluated undefined symbol: %s\n", expr->symbol->name);
  1226.       value = 0;
  1227.     }
  1228.   if (expr->bits > 0)
  1229.     return ((value >> 16) & 0xffff); /* Use upper bits of result */
  1230.   else if (expr->bits < 0)
  1231.     return (value & 0xffff);    /* Use lower bits */
  1232.   else
  1233.     return (value);
  1234. }
  1235.  
  1236.  
  1237. /* Print the EXPRESSION. */
  1238.  
  1239. #ifdef __STDC__
  1240. static int
  1241. print_imm_expr (char *buf, imm_expr *expr, int base_reg)
  1242. #else
  1243. static int
  1244. print_imm_expr (buf, expr, base_reg)
  1245.      char *buf;
  1246.      imm_expr *expr;
  1247.      int base_reg;
  1248. #endif
  1249. {
  1250.   char *bp = buf;
  1251.  
  1252.   if (expr->symbol != NULL)
  1253.     sprintf (buf, "%s", expr->symbol->name);
  1254.  
  1255.   buf += strlen(buf);
  1256.  
  1257.   if (expr->pc_relative)
  1258.     sprintf (buf, "-0x%08x", -expr->offset);
  1259.   else if (expr->offset < -10)
  1260.     sprintf (buf, "-%d (-0x%08x)", -expr->offset, -expr->offset);
  1261.   else if (expr->offset > 10)
  1262.     sprintf (buf, "+%d (0x%08x)", expr->offset, expr->offset);
  1263.  
  1264.   buf += strlen(buf);
  1265.  
  1266.   if (base_reg != -1 && expr->symbol != NULL &&
  1267.       (expr->offset > 10 || expr->offset < -10))
  1268.     {
  1269.       if (expr->offset == 0 && base_reg != 0)
  1270.     sprintf (buf, "+0");
  1271.       if (expr->offset != 0 || base_reg != 0)
  1272.     sprintf (buf, "($%d)", base_reg);
  1273.     }
  1274.   buf += strlen(buf);
  1275.  
  1276.   return (buf - bp);
  1277. }
  1278.  
  1279.  
  1280. /* Return non-zero if the EXPRESSION is a constant 0. */
  1281.  
  1282. #ifdef __STDC__
  1283. int
  1284. zero_imm (imm_expr *expr)
  1285. #else
  1286. int
  1287. zero_imm (expr)
  1288.      imm_expr *expr;
  1289. #endif
  1290. {
  1291.   return (expr->offset == 0 && expr->symbol == NULL);
  1292. }
  1293.  
  1294.  
  1295.  
  1296. /* Return an address expression of the form SYMBOL +/- IOFFSET (REGISTER).
  1297.    Any of the three parts may be omitted. */
  1298.  
  1299. #ifdef __STDC__
  1300. addr_expr *
  1301. make_addr_expr (long int offs, char *sym, int reg_no)
  1302. #else
  1303. addr_expr *
  1304. make_addr_expr (offs, sym, reg_no)
  1305.      long offs;
  1306.      char *sym;
  1307.      int reg_no;
  1308. #endif
  1309. {
  1310.   addr_expr *expr = (addr_expr *) xmalloc (sizeof (addr_expr));
  1311.   label *lab;
  1312.  
  1313.   if (reg_no == 0 && sym != NULL && (lab = lookup_label (sym))->gp_flag)
  1314.     {
  1315.       expr->reg_no = REG_GP;
  1316.       expr->imm = make_imm_expr (offs + lab->addr - gp_midpoint, NULL, 0);
  1317.     }
  1318.   else
  1319.     {
  1320.       expr->reg_no = reg_no;
  1321.       expr->imm = make_imm_expr (offs, (sym ? str_copy (sym) : sym), 0);
  1322.     }
  1323.   return (expr);
  1324. }
  1325.  
  1326.  
  1327. #ifdef __STDC__
  1328. imm_expr *
  1329. addr_expr_imm (addr_expr *expr)
  1330. #else
  1331. imm_expr *
  1332. addr_expr_imm (expr)
  1333. addr_expr *expr;
  1334. #endif
  1335. {
  1336.   return (expr->imm);
  1337. }
  1338.  
  1339.  
  1340. #ifdef __STDC__
  1341. int
  1342. addr_expr_reg (addr_expr *expr)
  1343. #else
  1344. int
  1345. addr_expr_reg (expr)
  1346. addr_expr *expr;
  1347. #endif
  1348. {
  1349.   return (expr->reg_no);
  1350. }
  1351.  
  1352.  
  1353.  
  1354. /* Map between a SPIM instruction and the binary representation of the
  1355.    instruction. */
  1356.  
  1357.  
  1358. /* Maintain a table mapping from internal opcode (i_opcode) to actual
  1359.    opcode (a_opcode).  Table must be sorted before first use since its
  1360.    entries are alphabetical on name, not ordered by opcode. */
  1361.  
  1362. static int sorted_i_opcode_table = 0; /* Non-zero => table sorted */
  1363.  
  1364.  
  1365. /* Map from internal opcode -> real opcode */
  1366.  
  1367. static inst_info i_opcode_tbl [] = {
  1368. #undef OP
  1369. #define OP(NAME, I_OPCODE, TYPE, A_OPCODE) {NAME, I_OPCODE, A_OPCODE},
  1370. #include "op.h"
  1371. };
  1372.  
  1373.  
  1374. /* Sort the opcode table on their key (the interal opcode value). */
  1375.  
  1376. #ifdef __STDC__
  1377. static void
  1378. sort_i_opcode_table (void)
  1379. #else
  1380. static void
  1381. sort_i_opcode_table ()
  1382. #endif
  1383. {
  1384.   qsort (i_opcode_tbl,
  1385.      sizeof (i_opcode_tbl) / sizeof (inst_info),
  1386.      sizeof (inst_info),
  1387.      (QSORT_FUNC) compare_pair_value);
  1388.   sorted_i_opcode_table = 1;
  1389. }
  1390.  
  1391.  
  1392. #define REGS(R,O) (((R) & 0x1f) << O)
  1393.  
  1394.  
  1395. #ifdef __STDC__
  1396. long
  1397. inst_encode (instruction *inst)
  1398. #else
  1399. long
  1400. inst_encode (inst)
  1401.      instruction *inst;
  1402. #endif
  1403. {
  1404.   long a_opcode = 0;
  1405.   inst_info *entry;
  1406.  
  1407.   if (inst == NULL)
  1408.     return (0);
  1409.   if (!sorted_i_opcode_table)
  1410.     sort_i_opcode_table ();
  1411.   if (!sorted_name_table)
  1412.     sort_name_table ();
  1413.  
  1414.   entry = map_int_to_inst_info (i_opcode_tbl,
  1415.                 sizeof (i_opcode_tbl) / sizeof (inst_info),
  1416.                 OPCODE (inst));
  1417.   if (entry == NULL)
  1418.     return 0;
  1419.  
  1420.   a_opcode = entry->value2;
  1421.   entry = map_int_to_inst_info (name_tbl,
  1422.                 sizeof (name_tbl) / sizeof (inst_info),
  1423.                 OPCODE (inst));
  1424.  
  1425.   switch (entry->value2)
  1426.     {
  1427.     case B0_TYPE_INST:
  1428.       return (a_opcode
  1429.           | (IOFFSET (inst) & 0xffff));
  1430.  
  1431.     case B1_TYPE_INST:
  1432.       return (a_opcode
  1433.           | REGS (RS (inst), 21)
  1434.           | (IOFFSET (inst) & 0xffff));
  1435.  
  1436.     case I1t_TYPE_INST:
  1437.       return (a_opcode
  1438.           | REGS (RS (inst), 21)
  1439.           | REGS (RT (inst), 16)
  1440.           | (IMM (inst) & 0xffff));
  1441.  
  1442.     case I2_TYPE_INST:
  1443.     case B2_TYPE_INST:
  1444.       return (a_opcode
  1445.           | REGS (RS (inst), 21)
  1446.           | REGS (RT (inst), 16)
  1447.           | (IMM (inst) & 0xffff));
  1448.  
  1449.     case I2a_TYPE_INST:
  1450.       return (a_opcode
  1451.           | REGS (BASE (inst), 21)
  1452.           | REGS (RT (inst), 16)
  1453.           | (IOFFSET (inst) & 0xffff));
  1454.  
  1455.     case R1s_TYPE_INST:
  1456.       return (a_opcode
  1457.           | REGS (RS (inst), 21));
  1458.  
  1459.     case R1d_TYPE_INST:
  1460.       return (a_opcode
  1461.           | REGS (RD (inst), 11));
  1462.  
  1463.     case R2td_TYPE_INST:
  1464.       return (a_opcode
  1465.           | REGS (RT (inst), 16)
  1466.           | REGS (RD (inst), 11));
  1467.  
  1468.     case R2st_TYPE_INST:
  1469.       return (a_opcode
  1470.           | REGS (RS (inst), 21)
  1471.           | REGS (RT (inst), 16));
  1472.  
  1473.     case R2ds_TYPE_INST:
  1474.       return (a_opcode
  1475.           | REGS (RS (inst), 21)
  1476.           | REGS (RD (inst), 11));
  1477.  
  1478.     case R2sh_TYPE_INST:
  1479.       return (a_opcode
  1480.           | REGS (RT (inst), 16)
  1481.           | REGS (RD (inst), 11)
  1482.           | REGS (SHAMT (inst), 6));
  1483.  
  1484.     case R3_TYPE_INST:
  1485.       return (a_opcode
  1486.           | REGS (RS (inst), 21)
  1487.           | REGS (RT (inst), 16)
  1488.           | REGS (RD (inst), 11));
  1489.  
  1490.     case R3sh_TYPE_INST:
  1491.       return (a_opcode
  1492.           | REGS (RS (inst), 21)
  1493.           | REGS (RT (inst), 16)
  1494.           | REGS (RD (inst), 11));
  1495.  
  1496.     case FP_I2a_TYPE_INST:
  1497.       return (a_opcode
  1498.           | REGS (BASE (inst), 21)
  1499.           | REGS (RT (inst), 16)
  1500.           | (IOFFSET (inst) & 0xffff));
  1501.  
  1502.     case FP_R2ds_TYPE_INST:
  1503.       return (a_opcode
  1504.           | REGS (FS (inst), 11)
  1505.           | REGS (FD (inst), 6));
  1506.  
  1507.     case FP_R2st_TYPE_INST:
  1508.       return (a_opcode
  1509.           | REGS (FT (inst), 16)
  1510.           | REGS (FS (inst), 11));
  1511.  
  1512.     case FP_R3_TYPE_INST:
  1513.       return (a_opcode
  1514.           | REGS (FT (inst), 16)
  1515.           | REGS (FS (inst), 11)
  1516.           | REGS (FD (inst), 6));
  1517.  
  1518.     case FP_MOV_TYPE_INST:
  1519.       return (a_opcode
  1520.           | REGS (FS (inst), 11)
  1521.           | REGS (FD (inst), 6));
  1522.  
  1523.     case J_TYPE_INST:
  1524.       return (a_opcode
  1525.           | TARGET (inst));
  1526.  
  1527.     case CP_TYPE_INST:
  1528.       return (a_opcode
  1529.           | REGS (RT (inst), 16)
  1530.           | REGS (RD (inst), 11));
  1531.  
  1532.     case NOARG_TYPE_INST:
  1533.       return (a_opcode);
  1534.  
  1535.     case ASM_DIR:
  1536.     case PSEUDO_OP:
  1537.     default:
  1538.       fatal_error ("Unknown instruction type in inst_encoding\n");
  1539.       return (0);        /* Not reached */
  1540.     }
  1541. }
  1542.  
  1543.  
  1544. /* Maintain a table mapping from actual opcode to interal opcode.
  1545.    Table must be sorted before first use since its entries are
  1546.    alphabetical on name, not ordered by opcode. */
  1547.  
  1548. static int sorted_a_opcode_table = 0; /* Non-zero => table sorted */
  1549.  
  1550.  
  1551. /* Map from internal opcode -> real opcode */
  1552.  
  1553. static inst_info a_opcode_tbl [] = {
  1554. #undef OP
  1555. #define OP(NAME, I_OPCODE, TYPE, A_OPCODE) {NAME, A_OPCODE, I_OPCODE},
  1556. #include "op.h"
  1557. };
  1558.  
  1559.  
  1560. /* Sort the opcode table on their key (the interal opcode value). */
  1561.  
  1562. #ifdef __STDC__
  1563. static void
  1564. sort_a_opcode_table (void)
  1565. #else
  1566. static void
  1567. sort_a_opcode_table ()
  1568. #endif
  1569. {
  1570.   qsort (a_opcode_tbl,
  1571.      sizeof (a_opcode_tbl) / sizeof (inst_info),
  1572.      sizeof (inst_info),
  1573.      (QSORT_FUNC) compare_pair_value);
  1574.   sorted_a_opcode_table = 1;
  1575. }
  1576.  
  1577.  
  1578. #define REG(V,O) ((V) >> O) & 0x1f
  1579.  
  1580.  
  1581. #ifdef __STDC__
  1582. instruction *
  1583.   inst_decode (unsigned long int value)
  1584. #else
  1585. instruction *
  1586.   inst_decode (value)
  1587. unsigned long value;
  1588. #endif
  1589. {
  1590.   long a_opcode = value & 0xfc000000;
  1591.   inst_info *entry;
  1592.   long i_opcode;
  1593.  
  1594.   if (a_opcode == 0)        /* SPECIAL */
  1595.     a_opcode |= (value & 0x3f);
  1596.   else if (a_opcode == 0x04000000) /* BCOND */
  1597.     a_opcode |= (value & 0x001f0000);
  1598.   else if (a_opcode == 0x40000000) /* COP0 */
  1599.     a_opcode |= (value & 0x03e00000) | (value & 0x1f);
  1600.   else if (a_opcode == 0x44000000) /* COP1 */
  1601.     {
  1602.       a_opcode |= (value & 0x03e00000);
  1603.       if ((value & 0xff000000) == 0x45000000)
  1604.     a_opcode |= (value & 0x00010000); /* BC1f/t */
  1605.       else
  1606.     a_opcode |= (value & 0x3f);
  1607.     }
  1608.   else if (a_opcode == 0x48000000 /* COPz */
  1609.        || a_opcode == 0x4c000000)
  1610.     a_opcode |= (value & 0x03e00000);
  1611.  
  1612.  
  1613.   if (!sorted_a_opcode_table)
  1614.     sort_a_opcode_table ();
  1615.   if (!sorted_name_table)
  1616.     sort_name_table ();
  1617.  
  1618.   entry = map_int_to_inst_info (a_opcode_tbl,
  1619.                 sizeof (a_opcode_tbl) / sizeof (inst_info),
  1620.                 a_opcode);
  1621.   if (entry == NULL)
  1622.     return (mk_r_inst (value, 0, 0, 0, 0, 0)); /* Invalid inst */
  1623.  
  1624.   i_opcode = entry->value2;
  1625.  
  1626.   switch (map_int_to_inst_info (name_tbl,
  1627.                 sizeof (name_tbl) / sizeof (inst_info),
  1628.                 i_opcode)->value2)
  1629.     {
  1630.     case B0_TYPE_INST:
  1631.       return (mk_i_inst (value, i_opcode, 0, 0, value & 0xffff));
  1632.  
  1633.     case B1_TYPE_INST:
  1634.       return (mk_i_inst (value, i_opcode, REG (value, 21), 0, value & 0xffff));
  1635.  
  1636.     case I1t_TYPE_INST:
  1637.       return (mk_i_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1638.              value & 0xffff));
  1639.  
  1640.     case I2_TYPE_INST:
  1641.     case B2_TYPE_INST:
  1642.       return (mk_i_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1643.              value & 0xffff));
  1644.  
  1645.     case I2a_TYPE_INST:
  1646.       return (mk_i_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1647.              value & 0xffff));
  1648.  
  1649.     case R1s_TYPE_INST:
  1650.       return (mk_r_inst (value, i_opcode, REG (value, 21), 0, 0, 0));
  1651.  
  1652.     case R1d_TYPE_INST:
  1653.       return (mk_r_inst (value, i_opcode, 0, 0, REG (value, 11), 0));
  1654.  
  1655.     case R2td_TYPE_INST:
  1656.       return (mk_r_inst (value, i_opcode, 0, REG (value, 16), REG (value, 11),
  1657.              0));
  1658.  
  1659.     case R2st_TYPE_INST:
  1660.       return (mk_r_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1661.              0, 0));
  1662.  
  1663.     case R2ds_TYPE_INST:
  1664.       return (mk_r_inst (value, i_opcode, REG (value, 21), 0, REG (value, 11),
  1665.              0));
  1666.  
  1667.     case R2sh_TYPE_INST:
  1668.       return (mk_r_inst (value, i_opcode, 0, REG (value, 16), REG (value, 11),
  1669.              REG (value, 6)));
  1670.  
  1671.     case R3_TYPE_INST:
  1672.       return (mk_r_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1673.              REG (value, 11), 0));
  1674.  
  1675.     case R3sh_TYPE_INST:
  1676.       return (mk_r_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1677.              REG (value, 11), 0));
  1678.  
  1679.     case FP_I2a_TYPE_INST:
  1680.       return (mk_i_inst (value, i_opcode, REG (value, 21), REG (value, 16),
  1681.              value & 0xffff));
  1682.  
  1683.     case FP_R2ds_TYPE_INST:
  1684.       return (mk_r_inst (value, i_opcode, REG (value, 11), 0, REG (value, 6),
  1685.              0));
  1686.  
  1687.     case FP_R2st_TYPE_INST:
  1688.       return (mk_r_inst (value, i_opcode, REG (value, 11), REG (value, 16), 0,
  1689.              0));
  1690.  
  1691.     case FP_R3_TYPE_INST:
  1692.       return (mk_r_inst (value, i_opcode, REG (value, 11), REG (value, 16),
  1693.              REG (value, 6), 0));
  1694.  
  1695.     case FP_MOV_TYPE_INST:
  1696.       return (mk_r_inst (value, i_opcode, REG (value, 11), 0, REG (value, 6),
  1697.              0));
  1698.  
  1699.     case J_TYPE_INST:
  1700.       return (mk_j_inst (value, i_opcode, value & 0x2ffffff));
  1701.  
  1702.     case CP_TYPE_INST:
  1703.       return (mk_r_inst (value, i_opcode, 0, REG (value, 16), REG (value, 11),
  1704.              0));
  1705.  
  1706.     case NOARG_TYPE_INST:
  1707.       return (mk_r_inst (value, i_opcode, 0, 0, 0, 0));
  1708.  
  1709.     case ASM_DIR:
  1710.     case PSEUDO_OP:
  1711.     default:
  1712.       return (mk_r_inst (value, 0, 0, 0, 0, 0)); /* Invalid inst */
  1713.     }
  1714. }
  1715.  
  1716.  
  1717. #ifdef __STDC__
  1718. static instruction *
  1719. mk_r_inst (unsigned long value, int opcode, int rs, int rt, int rd, int shamt)
  1720. #else
  1721. static instruction *
  1722. mk_r_inst (value, opcode, rs, rt, rd, shamt)
  1723.      unsigned long value;
  1724.      int opcode, rs, rt, rd, shamt;
  1725. #endif
  1726. {
  1727.   instruction *inst = (instruction *) zmalloc (sizeof (instruction));
  1728.  
  1729.   OPCODE (inst) = opcode;
  1730.   RS (inst) = rs;
  1731.   RT (inst) = rt;
  1732.   RD (inst) = rd;
  1733.   SHAMT (inst) = shamt;
  1734.   ENCODING (inst) = value;
  1735.   EXPR (inst) = NULL;
  1736.   return (inst);
  1737. }
  1738.  
  1739.  
  1740. #ifdef __STDC__
  1741. static instruction *
  1742. mk_i_inst (unsigned long value, int opcode, int rs, int rt, int offset)
  1743. #else
  1744. static instruction *
  1745. mk_i_inst (value, opcode, rs, rt, offset)
  1746.      unsigned long value;
  1747.      int opcode, rs, rt, offset;
  1748. #endif
  1749. {
  1750.   instruction *inst = (instruction *) zmalloc (sizeof (instruction));
  1751.  
  1752.   OPCODE (inst) = opcode;
  1753.   RS (inst) = rs;
  1754.   RT (inst) = rt;
  1755.   IOFFSET (inst) = offset;
  1756.   ENCODING (inst) = value;
  1757.   EXPR (inst) = NULL;
  1758.   return (inst);
  1759. }
  1760.  
  1761. #ifdef __STDC__
  1762. static instruction *
  1763. mk_j_inst (unsigned long value, int opcode, int target)
  1764. #else
  1765. static instruction *
  1766. mk_j_inst (value, opcode, target)
  1767.      unsigned long value;
  1768.      int opcode, target;
  1769. #endif
  1770. {
  1771.   instruction *inst = (instruction *) zmalloc (sizeof (instruction));
  1772.  
  1773.   OPCODE (inst) = opcode;
  1774.   TARGET (inst) = target;
  1775.   ENCODING (inst) = value;
  1776.   EXPR (inst) = NULL;
  1777.   return (inst);
  1778. }
  1779.  
  1780.  
  1781. /* Code to test encode/decode of instructions. */
  1782.  
  1783. #ifdef __STDC__
  1784. void
  1785. test_assembly (instruction *inst)
  1786. #else
  1787. void
  1788. test_assembly (inst)
  1789.      instruction *inst;
  1790. #endif
  1791. {
  1792.   instruction *new_inst = inst_decode (inst_encode (inst));
  1793.  
  1794.   inst_cmp (inst, new_inst);
  1795.   free_inst (new_inst);
  1796. }
  1797.  
  1798.  
  1799. #ifdef __STDC__
  1800. static void
  1801. inst_cmp (instruction *inst1, instruction *inst2)
  1802. #else
  1803. static void
  1804. inst_cmp (inst1, inst2)
  1805.      instruction *inst1, *inst2;
  1806. #endif
  1807. {
  1808.   char buf[1024];
  1809.  
  1810.   if (bcmp (inst1, inst2, sizeof (instruction) - 4))
  1811.     {
  1812.       printf ("=================== Not Equal ===================\n");
  1813.       print_inst_internal (buf, 1024, inst1, 0);
  1814.       printf ("%s\n", buf);
  1815.       print_inst_internal (buf, 1024, inst2, 0);
  1816.       printf ("%s\n", buf);
  1817.       printf ("=================== Not Equal ===================\n");
  1818.     }
  1819. }
  1820.